﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;

namespace Cofe.Data.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class DBVolumeRegistrarGetRootBehavior : IVolumeRegistrarGetRootBehavior
    {
        public IEnumerable<IPropertyHost> GetRootPropertyHosts()
        {
            var appliedVolumesFilter = CofeServices.VolumeRegistrar.LookupVolumes(v => true, false)
                                 .Select(v => new ParseNameEntryFilter("{" + v.VolumeId + "}")).ToList();
            
            using (var repository = CofeDBServices.RepositoryFactory.CreateRepository())
            {
                return repository.FindAll(Core.Filter.FilterCollections.FromFilters(
                    new OrEntryFilter(appliedVolumesFilter.ToArray())))
                    .ToList();
            }
           // return CofeServices.VolumeRegistrar.LookupVolumes(v => true, false).ToArray()
           //     .SelectMany(v => v.DirectoryLister.GetRootPropertyProviders())
           //.Select(pp => CofeServices.EntryConstructor.ConstructPropertyHost(pp)).ToList();
        }

        public EntryConstructionFlags[] SupportedConstructionFlags
        {
            get
            {
                return new[] {
                    EntryConstructionFlags.DefaultWithLookup,
                    EntryConstructionFlags.Default
                };
            }
        }
    }
}
